package club.ljwang.service;

import club.ljwang.dao.MemberMapper;
import club.ljwang.dao.OrderMapper;
import club.ljwang.pojo.ReportData;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wangl
 * @Date: 2020/3/7 22:34
 * @Description:
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public ArrayList<Integer> getMemberReport(ArrayList<String> months) {
        ArrayList<Integer> integers = new ArrayList<>();
        for (String month : months) {

            month = month + "-31";
            Integer integer = memberMapper.getMemberReport(month);
            integers.add(integer);

        }
        return integers;
    }

    @Override
    public ArrayList<Integer> getMemberReportByDays(ArrayList<String> days) {
        ArrayList<Integer> integers = new ArrayList<>();
        for (String day : days) {
            Integer integer = memberMapper.getMemberReport(day);
            integers.add(integer);
        }
        return integers;
    }

    @Override
    public List<Map> getSetmealReport() {


        return memberMapper.getSetmealReport();
    }

    @Override
    public ReportData getBusinessReportData() {
        String format = "yyyy-MM-dd";
        DateTime now = new DateTime();
        String nowStr = now.toString(format);
//        -- 本周的第一天
        String beginOfWeek = DateUtil.beginOfWeek(now).toString(format);
//        -- 本周的最后一天
        String endOfWeek = DateUtil.endOfWeek(now).toString(format);
//        -- 本月的第一天
        String beginOfMonth = DateUtil.beginOfMonth(now).toString(format);
//        -- 本月的最后一天
        String endOfMonth = DateUtil.endOfMonth(now).toString(format);
        ReportData reportData = new ReportData();
        reportData.setReportDate(nowStr);
        reportData.setTodayNewMember(memberMapper.findMemberCountByDate(nowStr));
        reportData.setTotalMember(memberMapper.findMemberTotalCount());
        reportData.setThisWeekNewMember(memberMapper.findMemberCountAfterDate(beginOfWeek));
        reportData.setThisMonthNewMember(memberMapper.findMemberCountAfterDate(beginOfMonth));

        reportData.setTodayOrderNumber(orderMapper.findOrderCountByDate(nowStr));
        reportData.setTodayVisitsNumber(orderMapper.findVisitsCountByDate(nowStr));

        reportData.setThisWeekOrderNumber(orderMapper.findOrderCountByBetweenDate(beginOfWeek, endOfWeek));
        reportData.setThisWeekVisitsNumber(orderMapper.findVisitsCountByBetweenDate(beginOfWeek, endOfWeek));

        reportData.setThisMonthOrderNumber(orderMapper.findOrderCountByBetweenDate(beginOfMonth, endOfMonth));
        reportData.setThisMonthVisitsNumber(orderMapper.findVisitsCountByBetweenDate(beginOfMonth, endOfMonth));
        reportData.setHotSetmeal(orderMapper.findHotSetmeal());

        return reportData;
    }

    @Override
    public Map getMemberCombo() {
        Map map = new HashMap();
        List<Map<String, Object>> sexList = memberMapper.countMemberSex();
        List<Map<String, Object>> ageList = memberMapper.countMemberAge();
        map.put("sex", sexList);
        map.put("age", ageList);
        return map;
    }


}
